/**
 * 收件箱功能
 * @author
 * @date 2012-11-20
 */

var colNames = ['', '发件人', '主题','内容','时间','','','','',''];

/**
 * colModel 显示列的属性
 *
 * @type {Object}
 */
var colModel = [
	{ name:'first', index:'first', width:"100%", align:"center" },
	{ name:'senderName',index:'senderName', hidden:true }, 
	{ name:'title',index:'title', hidden:true }, 
	{ name:'content',index:'content', hidden:true },  
	{ name:'formatCreateTime',index:'formatCreateTime', hidden:true },
	{ name:'receiveName',index:'receiveName', hidden:true },
	{ name:'senderId',index:'senderId', hidden:true },
	{ name:'receiveId',index:'receiveId', hidden:true },
	{ name:'receiverState',index:'receiverState', hidden:true },
	{ name:'id',index:'id', hidden:true },
];

/**
 * rowNum 想要显示的数据行数
 *
 * @type {Number}
 */

var rowNum = 15;

/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess = false;

$( "#inbox-content" ).jqGrid({
	url:'../message/loadInboxData.html',
	datatype:"json",
	mtype: "POST",
	//data: local_data,
	height: "auto",
	autowidth: true,
	colNames: colNames, 
	colModel: colModel,
	rowNum: rowNum, 
	viewrecords: true,
	multiselect: true, 
	multiboxonly: true,
	emptyrecords : '没有邮件',
	pager: 'pager', 
	jsonReader: {
		root: "result",
		page: "page",
		total: "total",
		records: "records",
		repeatitems: false,
		cell: "cell",
		id: "id"
	},
	
	gridComplete:function() {
		// 取消外边框
		$( "#gbox_inbox-content" ).css( "border", "none" );
		
		// 隐藏表头
		$( "#gview_inbox-content .ui-jqgrid-hdiv" ).css( "display", "none" );

		// 取消列间的分界限
		$( "#gview_inbox-content .ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );

		// 改变底边框样式
		$( "#gview_inbox-content .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
		
		// 取消最后一行的底边框
		$( "#gview_inbox-content .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
		
		// 设置表格行高
		//$( "#gview_inbox-content .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

		/**
		 * IDs 获取当前页所有数据的id
		 *
		 * @type {Object}
		 */

		var IDs = $( "#inbox-content" ).jqGrid( "getDataIDs" );
		//没有数据
		if(IDs.length==0){
			$("#pager_center").hide();
			$(".ui-paging-info").css("text-align","left");
			$(".ui-paging-info").css("font-size","15px");
			$(".ui-jqgrid-view").css("height","60px");
		}
		
		for (var i=0; i < IDs.length; i++) {

			/**
			 * everyRowData 根据某一行数据的id获取该数据所有信息
			 *
			 * @type {Object}
			 */

			var everyRowData = $( "#inbox-content" ).jqGrid( "getRowData", IDs[i] );
			
			// 收件箱信息
			metadata = '<div class="s-module">'; 
			if(everyRowData.receiverState=="UNREAD"){
				metadata = metadata + '<div class="s-new-img" id="inbox-img-'+everyRowData.id+'"></div>';
			}else{
				metadata = metadata + '<div class="s-old-img" id="inbox-img-'+everyRowData.id+'"></div>';
			}
			metadata = metadata + '<div class="sp-name">' + everyRowData.senderName + '</div>'
					+ '<div class="sp-title">' + everyRowData.title + '</div>'
					+ '<div class="sp-content">' + '<span>' + everyRowData.content + '</span>' + '</div>'
					+ '<div class="t1">' + everyRowData.formatCreateTime + '</div>'
					+'</div>';

			$( "#inbox-content" ).jqGrid( 'setRowData', IDs[i], { first: metadata } );
			// 取消提示信息
			$( ".metadata" ).parent("td").attr("title","");
			
			//点击返回进入收件箱页
			$("#inbox-back").click(function(){
				//详情页隐藏
				$(".s-detail").addClass("hide");
				$(".s-content").removeClass("hide");
			});
        }		
	},
	onSelectRow: function(rowid) {
		$(".s-content").addClass("hide");
		$(".s-detail").removeClass("hide");	
		//查看详情，加载详情信息
		var everyRowData = $( "#inbox-content" ).jqGrid( "getRowData", rowid );
		var id=everyRowData.id;
		
		var receiverState = everyRowData.receiverState;
		if(receiverState=="UNREAD"){
			$.ajax({ 
				type: "POST",  
	            url: "../message/changeState.html",  
	            data: "id="+id,  
	            contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
	            success: function() {  
	            	$("#inbox-img-"+id).removeClass("s-new-img");
	            	$("#inbox-img-"+id).addClass("s-old-img");
	            },  
	            error: function(err) {  
	                alert(err);  
	            }  
	        }); 
		}

		var senderName=everyRowData.senderName;
		var formatCreateTime=everyRowData.formatCreateTime;
		var receiveName=everyRowData.receiveName;
		var content=everyRowData.content;
		var title=everyRowData.title;
		//弹出发件人
		document.getElementById("senderName").innerHTML=senderName;
		document.getElementById('formatCreateTime').innerHTML=formatCreateTime;
		document.getElementById('receiveName').innerHTML=receiveName;
		document.getElementById('content').innerHTML=content;
		document.getElementById('title').innerHTML=title;
		$("#senderId").val(everyRowData.senderId);
		$("#receiveId").val(everyRowData.receiveId);
		$("#messageId").val(everyRowData.id);
		$("#receiveNicknames").val(senderName);
		$("#write_title").val("回复："+title);
		var content="\r\n\r\n\r\n\r\n"+"发件人："
		+senderName+"\r\n"+"创建时间："+formatCreateTime+"\r\n"
		+"收件人："+receiveName+"\r\n"+"主题："+title+"\r\n\r\n"+content;
		$("#write_content").val(content);
		//把数据插入到
		//替换信息图片为"已读"的图片
		$(this).childen("s-new-img").attr("class", "s-old-img");
		
	}
	/*
	//双击该行进入信息详情页
	ondblClickRow: function(rowid) {
	}
	*/
});

//点击回复,
$("#inbox-return").click(function(){
	$(".s-detail").addClass("hide");
	$(".s-return").removeClass("hide");
	
});
$(".s-write-return").click(function(){
	$(".s-return").addClass("hide");
	$(".s-detail").removeClass("hide");
});
$(".write_clean").click(function(){
	$("#write_content").val("");
});
$(".write_send").click(function(){
	var receiveId=$("#senderId").val();
	var title=$("#write_title").val();
	var content	=$("#write_content").val();
	$.ajax({ 
		type: "POST",  
        url: "../message/sendByOne.html",  
        data: "receiveId="+receiveId+"&title="+title+"&content="+content,  
        contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
        success: function() {  
        	alert("发送成功！");
        	self.location="../message/index.html";
        },  
        error: function(err) {  
            alert(err);  
        }  
    }); 
	
});
$(".write_backup").click(function(){
	var receiveId=$("#senderId").val();
	var receiveName=document.getElementById("senderName").innerHTML;
	var title=$("#write_title").val();
	var content	=$("#write_content").val();
	$.ajax({ 
		type: "POST",  
        url: "../message/saveAsDraft.html",  
        data: "receiveIds="+receiveId+"&title="+title+"&content="+content+"&draftReceiveNames="+receiveName,  
        contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
        success: function() {  
        	alert("保存到草稿箱！");
        },  
        error: function(err) {  
            alert(err);  
        }  
    }); 
});
$("#inbox-delete").click(function(){
	if (confirm("是否确认删除")){
	var id=$("#messageId").val();
	$.ajax({ 
		type: "POST",  
        url: "../message/deleteMessageByReceiver.html",  
        data: "id="+id,  
        contentType : "application/x-www-form-urlencoded;charset=UTF-8",  
        success: function() {  
        	alert("删除成功！");
        	 self.location="../message/index.html";
        },  
        error: function(err) {  
            alert(err);  
        }  
    }); 
	}
});
